package com.huoyun.cargo.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.huoyun.base.dao.BaseDaoImpl;
import com.huoyun.base.dao.evt.DaoBaseEvt;
import com.huoyun.cargo.bean.CargoInfo;
import com.huoyun.cargo.bean.CargoItem;
import com.huoyun.cargo.dao.evt.GetCargoByCityEvt;
import com.huoyun.cargo.dao.evt.GetCargosByUserIdEvt;
import com.huoyun.cargo.dao.evt.GetSimilarCargoByRouteEvt;
import com.huoyun.cargo.dao.evt.QueryCargoEvt;
import com.huoyun.cargo.dao.evt.SelectCargosForCarEvt;
import com.huoyun.cargo.model.CargoInfoModel;
import com.huoyun.cargo.model.CargoUserCompanyModel;

public class CargoInfoDaoImpl extends BaseDaoImpl<CargoInfo> implements
		CargoInfoDao {

	public long saveCargoInfo(CargoInfo cargoInfo) {
		// TODO Auto-generated method stub
		sqlSessionTemplate.insert(
				"com.huoyun.cargo.dao.CargoInfoDaoMapper.saveCargoInfo",
				cargoInfo);
		
		return cargoInfo.getId();
	}

	public void updateCargoInfo(CargoInfo cargoInfo) {
		// TODO Auto-generated method stub
		sqlSessionTemplate.update(
				"com.huoyun.cargo.dao.CargoInfoDaoMapper.updateCargoInfo",
				cargoInfo);
	}

	public CargoInfo getCargoInfo(CargoInfo cargoInfo) {
		return sqlSessionTemplate.selectOne(
				"com.huoyun.cargo.dao.CargoInfoDaoMapper.getCargoInfo",
				cargoInfo);
	}
	
	@Override
	public long getCargoCountByUserid(long userId)
	{
		return sqlSessionTemplate.selectOne("com.huoyun.cargo.dao.CargoInfoDaoMapper.getCargoCountByUserid", userId);
	}

	@Override
	public List<CargoInfoModel> getCargosByUserId(GetCargosByUserIdEvt cargosByUserIdEvt) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectList(
				"com.huoyun.cargo.dao.CargoInfoDaoMapper.getCargosByUserId",
				cargosByUserIdEvt);
	}

	@Override
	public CargoInfo getCargoInfoById(long cargoId) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectOne(
				"com.huoyun.cargo.dao.CargoInfoDaoMapper.getCargoInfoById", cargoId);
	}
	
	@Override
	public CargoInfo getCargoInfo(long userId, long cargoId)
	{
		Map<String, Object> dateMap = new HashMap<String, Object>();
		dateMap.put("userId", userId);
		dateMap.put("cargoId", cargoId);
		
		return sqlSessionTemplate.selectOne("com.huoyun.cargo.dao.CargoInfoDaoMapper.getCargoInfoByUserIdAndCargoId", dateMap);
	}

	@Override
	public List<CargoInfo> selectCargosByCondition(final CargoInfo cargoInfo) {
		return sqlSessionTemplate
				.selectList(
						"com.huoyun.cargo.dao.CargoInfoDaoMapper.queryCargoByCondition",
						cargoInfo);
	}

	@Override
	public List<CargoItem> selectCargosForCar(final com.huoyun.cargo.dao.evt.SelectCargosForCarEvt searchCriteria) {
		return sqlSessionTemplate.selectList("com.huoyun.cargo.dao.CargoInfoDaoMapper.queryCargoByForCar",searchCriteria);
	}

	@Override
	public int selectCargosForCarCount(SelectCargosForCarEvt searchCriteria) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectOne("com.huoyun.cargo.dao.CargoInfoDaoMapper.queryCargoByForCarCount", searchCriteria);
	}
	
	public CargoItem getCargoItemByGoodsId(long goodsId) {
		return sqlSessionTemplate.selectOne("com.huoyun.cargo.dao.CargoInfoDaoMapper.getCargoItemByGoodsId", goodsId);
	}
	
	/**
	 * 更新货物的状态
	 * @param userId 用户id
	 * @param goodsId 货物id
	 * @param status 更新的货物状态
	 * 
	 * @author 刘晓宁
	 */
	public void updateStatus(long userId, long goodsId, int status)
	{
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("userId", userId);
		dataMap.put("goodsId", goodsId);
		dataMap.put("status", status);
		
		this.sqlSessionTemplate.update("com.huoyun.cargo.dao.CargoInfoDaoMapper.updateStatus", dataMap);
	}
	
	public void delete(long goodsId) {
		// TODO Auto-generated method stub
		sqlSessionTemplate.delete("com.huoyun.cargo.dao.CargoInfoDaoMapper.delete",goodsId);
	}
	
	/**
	 * 将发票信息ID加入货物表
	 * @param invoiceId 发票ID
	 * @param cargoId  货物ID
	 * 
	 * @author 杨阳
	 */

	@Override
	public void updateInvoice(long invoiceId,long cargoId) {
		HashMap<String,Object> map=new HashMap<String,Object>();
		map.put("invoiceId", invoiceId);
		map.put("cargoId", cargoId);
		sqlSessionTemplate.update("com.huoyun.cargo.dao.CargoInfoDaoMapper.updateInvoice",map);
		
	}

	@Override
	public List<CargoInfo> getCargoByCity(GetCargoByCityEvt evt) {
		return sqlSessionTemplate.selectList("com.huoyun.cargo.dao.CargoInfoDaoMapper.getCargoByCity",evt);
	}

	@Override
	public long queryCargoCount(QueryCargoEvt queryCargoEvt) {
		return sqlSessionTemplate.selectOne("com.huoyun.cargo.dao.CargoInfoDaoMapper.queryCargoCount",queryCargoEvt);
	}
	/**
	 * 相关货源查询
	 * @param getSimilarCargoByRouteEvt
	 * @return
	 */
	
	public List<CargoInfo> getSimilarCargoByRoute(GetSimilarCargoByRouteEvt getSimilarCargoByRouteEvt){
		return sqlSessionTemplate.selectList("com.huoyun.cargo.dao.CargoInfoDaoMapper.getSimilarCargoByRoute",getSimilarCargoByRouteEvt);
	}

	@Override
	public long getSimilarCargoCount(
			GetSimilarCargoByRouteEvt getSimilarCargoByRouteEvt) {
		
		return sqlSessionTemplate.selectOne("com.huoyun.cargo.dao.CargoInfoDaoMapper.getSimilarCargoCount",getSimilarCargoByRouteEvt);
	}

	/**
	 * 更新货源的浏览次数
	 * @param viewCount 货源的浏览次数
	 * @param cargoId 货源id
	 */
	@Override
	public void updateViewCount(int viewCount, long cargoId) {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("cargId", cargoId);
		dataMap.put("viewCount", viewCount);
		
		sqlSessionTemplate.update("com.huoyun.cargo.dao.CargoInfoDaoMapper.updateViewCount", dataMap);
	}

	/**
	 * 查询最新的N条货物数据
	 * @param topN
	 * @return 
	 * @author yang
	 */
	@Override
	public List<CargoUserCompanyModel> getLatestNCargo(long topN) {
		return sqlSessionTemplate.selectList("com.huoyun.cargo.dao.CargoInfoDaoMapper.getLatestNCargo",topN);
	}

	@Override
	public List<CargoItem> getAll(DaoBaseEvt daoBaseEvt) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectList("com.huoyun.cargo.dao.CargoInfoDaoMapper.getAllCargoItem", daoBaseEvt);
	}

	@Override
	public int getAllCount() {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectOne("com.huoyun.cargo.dao.CargoInfoDaoMapper.getAllCargoItemCount");
	}
}
